// pages/mine/mine.js
let utils = require("../../utils/utils.js");
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		myPassword: "",
		motto: 'Hello World',
		userInfo: {},
		hasUserInfo: false,
		canIUse: wx.canIUse('button.open-type.getUserInfo'),
		goods_stutas: [{
				'name': '待付款',
				'icon': 'icon-daifukuan',
				'id': 0,
				'iconorder':'orderstatus01'
			},
			{
				'name': '待收货',
				'icon': 'icon-daishouhuo1',
				'id': 1,
				'iconorder':'orderstatus02'
			},
			{
				'name': '已收货',
				'icon': 'icon-yishouhuo',
				'id': 2,
				'iconorder':'orderstatus03'
			},
			{
				'name': '已取消',
				'icon': 'icon-yiquxiao',
				'id': 3,
				'iconorder':'orderstatus04'
			}
		],
		offering: []
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
	
	},
	
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		
		that.getuseravater();/* 获取个人积分信息 */
		that.offering() //充值列表数据
		if (userInfo) {
			console.log(userInfo.avatarUrl);
			console.log(0);
			that.setData({
				avatarUrl: userInfo.avatarUrl,
				hasUserInfo: true
			})
		}else{
			
		}
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {
	
	},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
	
	},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
	
	},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
	
	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
	
	},
	orderdetail(e) {
		let id = e.currentTarget.dataset.id;
		wx.navigateTo({
			'url': '../myorder/myorder?type=' + id
		})

	},
	mineinfo(e) {
		wx.navigateTo({
			url: '../changeinfo/changeinfo'
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		
	},
	/* 获取个人账户余额 */
	getuseravater(e) {
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		let _url = base_url + '/user/get?id=' + userInfo.userId;
		wx.request({
			url: _url,
			data: {},
			header: {
				'content-type': 'application/json', // 默认值
				'headerId': userInfo.openId,
				'headerValue': userInfo.headerValue
			},
			success: function(res) {
				console.log(res);
				if(res.data.status==200){
					let balance = res.data.data.balance;
					if (balance==0||balance==null) {
						
						balance = 0;
						balance=balance.toFixed(2);
					}
					that.setData({
						balance: balance,
						avatarUrl:res.data.data.imageUrl
					})
				}else{
					wx.showModal({
						title:"提示",
						content:res.data.msg,
						showCancel: true,
					})
				}
				
			},
		})
	},
	// 充值列表数据
	offering(e) {
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		let _url = base_url + '/charge/query';
		console.log(_url);
		console.log(userInfo);
		wx.request({
			url: _url,
			data: {},
			header: {
				'content-type': 'application/json', // 默认值
				'headerId': userInfo.openId,
				'headerValue': userInfo.headerValue
			},
			success: function(res) {
				console.log(res);
				if(res.data.status==200){
					let offering = res.data.data;
					that.setData({
						offering: offering
					})
				}else if(res.data.status==500){
					utils._login(res=>{
						console.log(res);
						let jsonuserinfo = res.data.data;
						if (jsonuserinfo.requireBind != true) {
							userInfo.headerValue = jsonuserinfo.sessionValue;
							wx.setStorageSync('userInfo', userInfo);
							that.offering();
						}
					});
				}
				
			}
		})
	},

	// 没有密码跳转到密码页
	password: function(e) {
		let that=this;
		let id=e.currentTarget.dataset.id;
		/* 获取密码 */
		that.myPassword(id);
		
		
	},

	// 当password的值不为null时，调用接口
	gocharge(id) {
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		let _url = base_url + '/charge/pay';
		console.log(_url,id,userInfo.openId);
		wx.request({
			url: _url,
			data: {
				ruleId: id,
				openId: userInfo.openId
			},
			header: {
				'content-type': 'application/json', // 默认值
				'headerId': userInfo.openId,
				'headerValue': userInfo.headerValue
			},
			success: function(res) {
				console.log(res);
				if(res.data.status==200){
					let pay = res.data.data;
					let timeStamp = pay.timeStamp;
					let packages = pay.package;
					let paySign = pay.paySign;
					let nonceStr = pay.nonceStr;
					let param = {
						"timeStamp": timeStamp,
						"package": packages,
						"paySign": paySign,
						"signType": "MD5",
						"nonceStr": nonceStr
					};
					/* 支付 */
					that.pay(param);
				}
			}
		})
	},
	confirmcharge(e) {
		let that = this;
		//生成商户订单
		let remark = that.data.remark;
		if (remark == '') {
			remark = '暂无备注';
		}
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		let _url = base_url + '/wxPay/pay';
		let order = that.data.order;
		let freightCharge = that.data.yunfei;
		let payAmount = that.data.ordertotalprice;
		let addressId = that.data.addres.id;
		let deliveryType = that.data.deliveryType;
		wx.request({
			url: _url,
			method: 'post',
			data: {
				payAmount: payAmount, //付款金额
				deliveryType: 0, //配送方式  0：平台配送 1：自取
				freightCharge: freightCharge, //订单运费
				addressId: addressId, //收货地址id
				orderNumber: order.orderNumber, //唯一订单号
				orderAmount:order.orderAmount,
				buyerOpenId: userInfo.openId
			},
			header: {
				'content-type': 'application/x-www-form-urlencoded', // 默认值
				'headerId': userInfo.openId,
				'headerValue': userInfo.headerValue
			},
			success: function(res) {
				console.log(res);
				let pay = res.data.data;
				
			},
		})
	},
	//获取password的值
	myPassword(id) {
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		let _url = base_url + '/user/get?id=' + userInfo.userId;
		wx.request({
			url: _url,
			data: {},
			header: {
				'content-type': 'application/json', // 默认值
				'headerId': userInfo.openId,
				'headerValue': userInfo.headerValue
			},
			success: function(res) {
				console.log(res.data);
				if(res.data.status==200){
					let myPassword = res.data.data.password;
					if(myPassword){
						wx.showModal({
							title: "提示",
							content: "确认充值?",
							showCancel: true,
							success: function(res) {
								if (res.confirm) {
									/*订单支付函数*/
									that.gocharge(id);
								} else if (res.cancel) {
									console.log('用户点击取消');
									return false;
								}
							}
						});
						
					}else{
						wx.redirectTo({
							'url':'../password/password'
						})
					}
				}
			}
		})
	},
	/* 支付  */
	pay(param) {
		let that=this;
		wx.requestPayment({
			timeStamp: param.timeStamp,
			nonceStr: param.nonceStr,
			package: param.package,
			signType: param.signType,
			paySign: param.paySign,
			success: function(res) {
				console.log('支付信息');
				console.log(res);
				//return false;
				if (res.errMsg == "requestPayment:ok") { // 调用支付成功
				    that.getuseravater();/* 获取个人信息 */
					wx.showToast({
					  title: '已充值成功',
					  icon: 'none'
					})
					
				} else if (res.errMsg == 'requestPayment:cancel') {
					wx.showToast({
					  title: '已取消充值',
					  icon: 'none'
					})
				}
			},
			fail: function(res) {
				// fail
			},
			complete: function() {
				// complete
			}
		})
	}
	
})
